using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace Googler.BLL
{
    /// <summary>
    /// Summary description for Resources.
    /// </summary>
    public class Resources
    {
        static string _LastAddedResourcePath;
        static string _LastAddedResourceName;

        public string LastAddedResourcePath
        {
            get
            {
                return _LastAddedResourcePath;
            }
        }


        public string LastAddedResourceName
        {
            get
            {
                return _LastAddedResourceName;
            }
        }

        public Resources()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void Remove(string filePath)
        {
            File.Delete(filePath);
        }
        public string Add(System.Web.UI.HtmlControls.HtmlInputFile File, string targetFolder)
        {

            if (File.Value == "")
            {
                return "Click 'Browse' to select the file to upload.";
            }
            //Needed code to check that the file is image
            else
            {

                targetFolder += targetFolder.EndsWith("\\") ? "" : "\\";
                string fileName = Path.GetFileName(File.PostedFile.FileName).Replace(" ", "_");

                int backSlachIndex = targetFolder.IndexOf("\\", 3);
                while (backSlachIndex != -1)
                {
                    string tmp = targetFolder.Substring(0, backSlachIndex);
                    if (!System.IO.Directory.Exists(targetFolder.Substring(0, backSlachIndex)))
                    {
                        System.IO.Directory.CreateDirectory(targetFolder.Substring(0, backSlachIndex));
                    }
                    backSlachIndex = targetFolder.IndexOf("\\", ++backSlachIndex);
                }
                //				if(System.IO.File.Exists(targetFolder + fileName))
                //				{
                //					return "This File Already Exist";
                //				}

                File.PostedFile.SaveAs(targetFolder + fileName);
                _LastAddedResourcePath = targetFolder + fileName;
                _LastAddedResourceName = fileName;
                return "";
            }
        }

        public string AddByID(System.Web.UI.HtmlControls.HtmlInputFile oFile, string FileNameID, string strFolder, string Category, string fileCategory)
        {
            string FileSize = "";


            string strFileExt;

            string strFilePath;

            // Retrieve the name of the file that is posted.
            string strFileName = oFile.PostedFile.FileName;
            strFileName = Path.GetFileName(strFileName);
            strFileExt = Path.GetExtension(strFileName);

            // providing the folder that the file will be saved in

            strFolder += strFolder.EndsWith("\\") ? fileCategory : (fileCategory + "\\");

            //			try
            //			{

            if (fileCategory != "")
            {
                if (FileSize != "notAllowed")
                {
                    if (oFile.Value != "")
                    {
                        // Create the folder if it does not exist.
                        if (!Directory.Exists(strFolder))
                        {
                            Directory.CreateDirectory(strFolder);
                        }
                        //check if the file is already exist
                        //						if(File.Exists(strFilePath1))
                        //						{
                        //							//Err1
                        //							return "Already exists";
                        //						}
                        //						else
                        //						{
                        strFileName = Category + FileNameID + strFileExt;
                        strFilePath = strFolder + strFileName;
                        oFile.PostedFile.SaveAs(strFilePath);
                        return strFileName;
                        //						}

                    }
                    else
                    {
                        return "Click 'Browse' to select the file to upload.";
                    }
                }
                else
                {
                    return "The Image File is more than 100 KB";
                }
            }
            return "";
        }

        public static string AddFile(System.Web.UI.HtmlControls.HtmlInputFile File, string FilePath)
        {
            if (File.Value == "")
            {
                return "Click 'Browse' to select the file to upload.";
            }
            if (!Directory.Exists(FilePath))
            {
                Directory.CreateDirectory(FilePath);
            }
            string text1 = Path.GetFileName(File.PostedFile.FileName).Replace(' ', '_');
            Path.GetExtension(File.PostedFile.FileName);
            FilePath = FilePath + (FilePath.EndsWith(@"\") ? "" : @"\");
            string text2 = FilePath + @"\" + text1;
            File.PostedFile.SaveAs(text2);
            return text1;
        }

 

    }
}

